#include "Bus.h"
#include "Listener.h"
#include "Common.h"

namespace wsbus {

	Bus::Bus(unsigned short port)
		: port_(port)
	{
	}

	void Bus::run()
	{
		unsigned short validPort = port_;
		do {
			auto endpoint = tcp::endpoint{ net::ip::make_address("127.0.0.1"), validPort };
			auto threadCount = 3;
			auto listener = std::make_shared<Listener>(std::move(endpoint), threadCount);
			if (listener->isBindSuccess()) {
				saveBusPort(validPort);
				listener->run();
				break;
			}
			validPort++;
		} while (1);
	}

}
